package com.xaaef.grpc.lib.util;

import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.time.Instant;

/**
 * <p>
 * RESTful API 统一返回实体类
 * </p>
 */

@Getter
@Setter
public class RestResult<T> implements Serializable {

    private int status;

    private long timestamp;

    private String message;

    private T data;

    private RestResult() {
    }


    /**
     * 自定义返回状态码
     *
     * @param status
     * @param timestamp
     * @param message
     * @param data
     */
    public static <T> RestResult<T> result(int status, long timestamp, String message, T data) {
        var result = new RestResult<T>();
        result.status = status;
        result.timestamp = timestamp;
        result.message = message;
        result.data = data;
        return result;
    }

    /**
     * 自定义返回状态码
     *
     * @param timestamp
     * @param timestamp
     * @param message
     */
    public static <T> RestResult<T> success(long timestamp, String message, T data) {
        return result(HttpStatus.OK.value(), timestamp, message, data);
    }

    /**
     * 成功
     *
     * @param message
     * @param data
     */
    public static <T> RestResult<T> success(String message, T data) {
        return success(Instant.now().toEpochMilli(), message, data);
    }


    /**
     * 成功
     *
     * @param data
     */
    public static <T> RestResult<T> success(T data) {
        return success(Instant.now().toEpochMilli(), "ok", data);
    }


}
